package com.ruoyi.job.handler;

import com.ruoyi.job.service.SysUserFeignService;
import com.ruoyi.job.service.TraceTransportFeignService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.domain.TraceTransport;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: blockchain-fabric-group1
 * @package: com.ruoyi.job.handler
 * @className: XxlJobHandler
 * @author: 刘乘滨
 * @description: TODO
 * @date: 2023/05/18 14:18
 * @version: 1.0
 */
@Component
public class XxlJobHandler {

    @Autowired
    SysUserFeignService sysUserFeignService;

    @Autowired
    TraceTransportFeignService traceTransportFeignService;

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${sms.AppId}")
    private  String appId;
    @Value("${sms.AppSecret}")
    private  String appSecret;
    @Value("${sms.appUrl}")
    private  String appUrl;

    /**
     * 每天查询当天过生日所有员工并发送祝福邮件
     */
    @XxlJob("userJobHandler")
    public void userBirth() {
        List<SysUser> list = sysUserFeignService.sheng();

        SimpleMailMessage simpleMailMessage =new SimpleMailMessage();
        simpleMailMessage.setFrom("2420963310@qq.com");

        for (SysUser sysUser : list) {
            System.err.println(sysUser.getNickName()+"今天过生日,邮箱是"+sysUser.getEmail());

            simpleMailMessage.setTo(sysUser.getEmail());
            simpleMailMessage.setSubject("生日祝福");
            simpleMailMessage.setText("一份祝福，一份真诚，在这个特别的日子里，公司全体同仁给您送上生日最最真挚的祝福，祝您生日快乐家庭幸福!");
            simpleMailMessage.setSentDate(new Date());

            javaMailSender.send(simpleMailMessage);

        }

    };


    /**
     * 对两天没有发货的物流司机进行短信提醒
     */
    @XxlJob("listByTimeAndStatus")
    public void listByTimeAndStatus(){
        List<TraceTransport> traceTransports = traceTransportFeignService.listByTimeAndStatus();

        if(null != traceTransports) {
            ZhenziSmsClient client = new ZhenziSmsClient(appUrl, appId, appSecret);

            for (TraceTransport traceTransport : traceTransports) {
                System.err.println("注意：" + traceTransport.getCropsId() + "货物已两天未发货，请尽快处理");

                SysUser user = sysUserFeignService.getByUsername(traceTransport.getFarmerNickName());

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("number", user.getPhonenumber());

                //11691   您有货物{1}{2}{3}{4}已两天未发货，请尽快处理
                //11379   您的订单{1}{2}已支付
                //11380   您的{1}{2}订单还未支付，请及时处理
                //11074   验证码:{1}，{2}分钟内有效，请勿泄漏给他人使用。

                params.put("templateId", "11380");
                String[] templateParams = new String[1];

                templateParams[0] = traceTransport.getCropsId().substring(0, 11);
                templateParams[1] = traceTransport.getCropsId().substring(11);

                params.put("templateParams", templateParams);

                try {
                    String s = client.send(params);

                    System.err.println(s);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }else{
            System.err.println("目前没有两天未发货物流信息");
        }
    }

}
